------gc_num 累计总数                   取的时候记得要删除
------gc_numbysec_00-59 按秒累计
------gc_numbyhour_00-23
------gc_wznumbysec_00-59
------gc_wznumbyhour_00-59
------gc_numbyday_00-31
------gc_wznumbyday_00-31
------gc_numbymon_2014-08
------gc_wznumbymon_2014-08
------gc_lkbh_numbyday_00-31
------gc_lkbh_wznumbyday_00-31
------ip_enddata            每个设备最后一条上传记录
----每天凌晨 自动删除按秒累计key值、违章
---------------按小时累计key值、违章
---------------按天累计key值、违章
---------------并将按天累计key值插入数据库
local db_method = require "gcs.db.method"
local red_method = require "gcs.list.method"
local yesterday
local res = io.popen("date -d yesterday +%Y-%m-%d")
for line in res:lines() do
  yesterday = line   --取昨天日期
end
local end_num = string.len(yesterday)
local Yes_date = string.sub(yesterday,end_num-1,end_num) --取昨天日期后2位
local dic={"N","W","S","E"}



local tb_key = {}
tb_key[1] = "gc_numbyday_"..Yes_date
tb_key[2] = "gc_wznumbyday_"..Yes_date
local tb_key1,err = red_method.get_data(tb_key)
if not tb_key1 then
  tb_key1 = {}
end
local gc_numbyday = tb_key1["gc_numbyday_"..Yes_date] or ""
local gc_wznumbyday = tb_key1["gc_wznumbyday_"..Yes_date] or ""
local query = "insert into countNum (gc_numbyday,gc_wznumbyday,time) values "
query = query .. "("..ngx.quote_sql_str(gc_numbyday)..","..ngx.quote_sql_str(gc_wznumbyday)..","..ngx.quote_sql_str(yesterday)..");"
db_method.query(query)
for i=0,59 do       ---删除key值
  local tbkey = {}
  if string.len(tostring(i)) == 1 then
      tbkey[1] = "gc_numbysec_0"..i
      tbkey[2] = "gc_numbyhour_0"..i
      tbkey[3] = "gc_wznumbysec_0"..i
      tbkey[4] = "gc_wznumbyhour_0"..i
      tbkey[5] = "gc_numbyday_0"..i
      tbkey[6] = "gc_wznumbyday_0"..i
      tbkey[7] = "date_send_success_num_0"..i
      tbkey[8] = "date_send_num_0"..i
      
  else
      tbkey[1] = "gc_numbysec_"..i
      tbkey[2] = "gc_numbyhour_"..i
      tbkey[3] = "gc_wznumbysec_"..i
      tbkey[4] = "gc_wznumbyhour_"..i
      tbkey[5] = "gc_numbyday_"..i
      tbkey[6] = "gc_wznumbyday_"..i
      tbkey[7] = "date_send_success_num_"..i
      tbkey[8] = "date_send_num_"..i
    
  end
  local res,err = red_method.del_data(tbkey)
  if not res then
    ngx.say(err)
  end
end  
local sql="select lkbh from base_lkxx " 
local res,err=db_method.query(sql)
if not res then
    ngx.say(err)
else
   for _,v in pairs(res) do
      for i=0,59 do
          for _,d in pairs(dic) do
              local tbkey = {}
              if string.len(tostring(i)) == 1 then
                  tbkey[1] =v.lkbh .. "_gc_1car" ..d .."_numbyhour_0" ..i
                  tbkey[2] =v.lkbh .. "_gc_23car" ..d .."_numbyhour_0" ..i
                  tbkey[3] =v.lkbh .. "_gc_2car" ..d .."_numbyhour_0" ..i
                  tbkey[4] =v.lkbh .. "_gc_99car" ..d .."_numbyhour_0" ..i
                  tbkey[5] =v.lkbh .. "_gc_natcar" ..d .."_numbyhour_0" ..i
                  tbkey[6] =v.lkbh .. "_gc_waicar" ..d .."_numbyhour_0" ..i
                  tbkey[7] =v.lkbh .. "_gc_" ..d .."numbyhour_0" ..i
                  tbkey[8] =v.lkbh .. "_gc_othercar" ..d .."_numbyhour_0" ..i
                  tbkey[9] =v.lkbh .. "_gc_wznumbyhour_0" ..i
                  tbkey[10] =v.lkbh .. "_gc_numbyhour_0" ..i
                  
              else
                  tbkey[1] =v.lkbh .. "_gc_1car" ..d .."_numbyhour_" ..i
                  tbkey[2] =v.lkbh .. "_gc_23car" ..d .."_numbyhour_" ..i
                  tbkey[3] =v.lkbh .. "_gc_2car" ..d .."_numbyhour_" ..i
                  tbkey[4] =v.lkbh .. "_gc_99car" ..d .."_numbyhour_" ..i
                  tbkey[5] =v.lkbh .. "_gc_natcar" ..d .."_numbyhour_" ..i
                  tbkey[6] =v.lkbh .. "_gc_waicar" ..d .."_numbyhour_" ..i
                  tbkey[7] =v.lkbh .. "_gc_othercar" ..d .."_numbyhour_" ..i
                  tbkey[8] =v.lkbh .. "_gc_" ..d .."numbyhour_" ..i
                  tbkey[9] =v.lkbh .. "_gc_wznumbyhour_" ..i
				  tbkey[10] =v.lkbh .. "_gc_numbyhour_" ..i
              end
              local res,err = red_method.del_data(tbkey)
          end
      end
   end 
end
















